﻿<local:AudioTimeControlBase x:Class="LaunchPad.Core.Controls.Tracks.TrackStackControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:LaunchPad.Core.Controls.Tracks"
             xmlns:core="clr-namespace:LaunchPad.Core"
                            xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
                            mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300" SnapsToDevicePixels="False"
                            local:AudioTimeControlBase.BeginCueHelperMove="AudioTimeControlBaseBeginCueHelperMove"
                            local:AudioTimeControlBase.CueHelperMove="AudioTimeControlBaseCueHelperMove"
                            local:AudioTimeControlBase.EndCueHelperMove="AudioTimeControlBaseEndCueHelperMove">
    <local:AudioTimeControlBase.Resources>
        
        <CollectionViewSource x:Key="CollectionViewSource" Source="{Binding}">
            <CollectionViewSource.SortDescriptions>
                <scm:SortDescription PropertyName="Timeline.Start" Direction="Ascending"/>
            </CollectionViewSource.SortDescriptions>
        </CollectionViewSource>
        
        <Style x:Key="GroupHeaderStyle" TargetType="{x:Type GroupItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type GroupItem}">
                        <Expander IsExpanded="True">
                            <Expander.Header>
                                <TextBlock Text="{Binding Name}" FontWeight="Bold" SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical" />
                            </Expander.Header>
                            <ItemsPresenter />
                        </Expander>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <DataTemplate DataType="{x:Type core:TimeTrack}">
            <local:TimeControl/>
        </DataTemplate>
        <DataTemplate DataType="{x:Type core:EffectTrack}">
            <local:EffectSliderControl Events="{Binding Timeline}"/>
        </DataTemplate>
    </local:AudioTimeControlBase.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="100"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <local:WaveViewerControl x:Name="_waveView" VerticalAlignment="Stretch" />
        
        <ScrollViewer x:Name="_scrollViewer" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled" Grid.Row="1" >
            <DataGrid Name="_trackDataGrid" ItemsSource="{Binding}" AutoGenerateColumns="False" CanUserAddRows="False"
                      HeadersVisibility="Row" RowHeight="30"  RowHeaderWidth="74" SelectedCellsChanged="EffectTrackSelectedCellsChanged"
                      AllowDrop="True" DragEnter="EffectTrackDragEnter" DragOver="EffectTrackDragOver" Drop="EffectTrackDrop"
                      MouseDoubleClick="EffectTrackMouseDoubleClick" HorizontalScrollBarVisibility="Auto" Height="{Binding ElementName=_scrollViewer, Path=ActualHeight}"
                      KeyDown="EffectTrackKeyDown">
                <DataGrid.GroupStyle>
                    <GroupStyle ContainerStyle="{StaticResource GroupHeaderStyle}">
                        <GroupStyle.Panel>
                            <ItemsPanelTemplate>
                                <DataGridRowsPresenter/>
                            </ItemsPanelTemplate>
                        </GroupStyle.Panel>
                    </GroupStyle>
                </DataGrid.GroupStyle>
                <DataGrid.CellStyle>
                    <Style TargetType="{x:Type DataGridCell}">
                        <Style.Triggers>
                            <Trigger Property="DataGridCell.IsSelected" Value="True">
                                <Setter Property="Background" Value="#CCDAFF" />
                            </Trigger>
                            <DataTrigger Binding="{Binding IsSelected}" Value="True">
                                <Setter Property="DataGridCell.IsSelected" Value="True"/>
                            </DataTrigger>
                            <DataTrigger Binding="{Binding IsSelected}" Value="False">
                                <Setter Property="DataGridCell.IsSelected" Value="False"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </DataGrid.CellStyle>
                <DataGrid.RowHeaderTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="12"/>
                                <RowDefinition Height="14"/>
                            </Grid.RowDefinitions>
                            <TextBlock Text="{Binding Path=Item.Name, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}}"
                                   FontWeight="Bold" FontSize="10"/>
                            <TextBlock Text="{Binding Path=Item.Address, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}}"
                                   FontWeight="Bold" FontSize="10" Grid.Row="1"/>
                        </Grid>
                    </DataTemplate>
                </DataGrid.RowHeaderTemplate>
                <DataGrid.Columns>
                    <DataGridTemplateColumn x:Name="_timelineColumn" SortMemberPath="Timeline.Start" SortDirection="Descending">

                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <!-- The cell template is a ContentPresenter, hence the actual
                 template will be selected by type. -->
                                <ContentPresenter Content="{Binding}"/>
                            </DataTemplate>

                            <!--<DataTemplate>
                                <local:EffectSliderControl Events="{Binding Timeline}"/>
                            </DataTemplate>-->
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>
        </ScrollViewer>

        <Canvas Name="_cueHelperCanvas" Margin="76,0,0,0" HorizontalAlignment="Left" Grid.Row="0" Grid.RowSpan="2" >
            <local:CueHelperControl x:Name="_cueHelperControl" VerticalAlignment="Stretch" Height="Auto" Visibility="Hidden" Focusable="False"/>
        </Canvas>

        <Slider Name="_zoomSlider" ValueChanged="ZoomSliderValueChanged" Grid.Row="2" HorizontalAlignment="Right" Width="100" Value="1" Minimum="1" Margin="3" />
        <Button Name="_sortButton" Grid.Row="2" Content="Sort" HorizontalAlignment="Left" Width="50" Click="_sortButton_OnClick" Margin="3"/>
    </Grid>
</local:AudioTimeControlBase>